// TeacherRepository.java
package com.example.orderfood2.Repository;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.example.orderfood2.entity.Student;
import com.example.orderfood2.entity.Teacher;

import java.util.ArrayList;
import java.util.List;

public class TeacherRepository extends BaseRepository {
    
    public TeacherRepository(Context context) {
        super(context);
    }

    public long registerTeacher(Teacher teacher) {
        open();
        ContentValues values = createContentValues();
        values.put("username", teacher.getUsername());
        values.put("password", teacher.getPassword());
        values.put("sex", teacher.getSex());
        values.put("title", teacher.getTitle());
        values.put("date", teacher.getDate());
        values.put("science", teacher.getScience());
        long result = database.insert("teacher", null, values);
        close();
        return result;
    }

    public boolean loginTeacher(String username, String password) {
        open();
        boolean result = false;
        Cursor cursor = database.query("teacher", null, "username=?", 
                new String[]{username}, null, null, null);
        
        if (cursor != null) {
            try {
                if (cursor.moveToFirst()) {
                    String dbPassword = cursor.getString(cursor.getColumnIndexOrThrow("password"));
                    result = dbPassword.equals(password);
                }
            } finally {
                cursor.close();
            }
        }
        close();
        return result;
    }
    //    teacher表的id,username的查
//    public List<Teacher> getAllTeachers() {
//        open();
//        List<Teacher> teachers = new ArrayList<>();
//        Cursor cursor = database.rawQuery("SELECT * FROM teacher", null);
//
//        if (cursor != null) {
//            try {
//                while (cursor.moveToNext()) {
//                    Teacher teacher = new Teacher(
//                            cursor.getInt(0),
//                            cursor.getString(1),
//                            cursor.getString(2),
//                            cursor.getString(3),
//                            cursor.getString(4),
//                            cursor.getString(5),
//                            cursor.getString(6)
//                    );
//                    teachers.add(teacher);
//                }
//            } finally {
//                cursor.close();
//            }
//        }
//        close();
//        return teachers;
//    }

    // 其他Teacher相关的方法...
    public boolean updateTeacher(int id, Teacher teacher) {
        open();
        ContentValues values = createContentValues();
        values.put("username", teacher.getUsername());
        values.put("password", teacher.getPassword());
        values.put("sex", teacher.getSex());
        values.put("title", teacher.getTitle());
        values.put("date", teacher.getDate());
        values.put("science", teacher.getScience());
        boolean result = database.update("teacher", values, "id=?", 
                new String[]{String.valueOf(id)}) > 0;
        close();
        return result;
    }

    public boolean deleteTeacher(int id) {
        open();
        boolean result = database.delete("teacher", "id=?", 
                new String[]{String.valueOf(id)}) > 0;
        close();
        return result;
    }

    //teacher找到对应的学生
    public List<Student> getStudentsByTeacherScience(String science){
        open();
        List<Student> studentList=new ArrayList<>();
        // 先根据science获取classnumber
        Cursor classCursor=database.rawQuery("SELECT classnumber FROM class_info WHERE classname =?",new String[]{science});
        while (classCursor.moveToNext()){
            String classnumber=classCursor.getString(0);
            // 再根据classnumber获取学生信息
            Cursor studentCursor = database.query("student", null, "classId =?", new String[]{classnumber}, null, null, null);
            if(studentCursor!=null){
                try {
                    while (studentCursor.moveToNext()) {
                        int id = studentCursor.getInt(0);
                        String username = studentCursor.getString(1);
                        String password = studentCursor.getString(2);
                        String sex = studentCursor.getString(3);
                        String classId = studentCursor.getString(4);
                        String score = studentCursor.getString(5);
                        String date = studentCursor.getString(6);
                        String enrol = studentCursor.getString(7);
                        Student student = new Student(id, username, password, sex, classId, score, date, enrol);
                        studentList.add(student);
                    }
                } finally {
                    studentCursor.close();
                }
            }
        }
        if (classCursor != null) {
            classCursor.close();
        }
        close();
        return studentList;
    }

    //    teacher的密码修改
    public int TeacherChangePassword(String username,String password){
        open();
        ContentValues T=new ContentValues();
        T.put("password",password);
        int teacher=database.update("teacher",T,"username like ?",new String[]{username});
        close();
        return teacher;
    }

    //    teacher表的id,username的查
    public Cursor getAllTeachers(){
        open();
        Cursor cursor = database.rawQuery("SELECT id, username FROM teacher", null);
//        close();
        return cursor;
    }

    //    通过id来查找teacher表
    public Teacher getTeacherById(int id){
        open();
        Cursor cursor=database.query("teacher",
                null,
                "id=?",
                new String[]{String.valueOf(id)},
                null, null, null);
        if(cursor!=null&&cursor.moveToFirst()){
            Teacher teacher = new Teacher(
                    cursor.getInt(0),
                    cursor.getString(1),
                    cursor.getString(2),
                    cursor.getString(3),
                    cursor.getString(4),
                    cursor.getString(5),
                    cursor.getString(6));
            cursor.close();
            close();
            return teacher;
        };
        close();
        return null;
    }
    //通过username来查询teacher表
    public Teacher getTeacherByUsername(String username){
        open();
        Cursor cursor=database.query("teacher",null,"username=?",new String[]{username},null,null,null);
        if(cursor!=null&&cursor.moveToFirst()){
            Teacher teacher=new Teacher(
                    cursor.getInt(0),
                    cursor.getString(1),
                    cursor.getString(2),
                    cursor.getString(3),
                    cursor.getString(4),
                    cursor.getString(5),
                    cursor.getString(6));
            cursor.close();
            close();
            return teacher;
        };
        close();
        return null;
    }
}